import { getMenuTree } from "@/api/auth";
import type { MenuTree } from "@/api/auth/typing";
import { generate } from "@/router/generate-router";
import { defineStore } from "pinia";

const menus: MenuTree = [];
export const useMenuStore = defineStore("menu", {
  state: () => {
    return { menus, loading: false };
  },
  actions: {
    async getMenuTree() {
      this.loading = true;
      const response = await getMenuTree();
      const { success, result } = response;
      if (success) {
        this.menus = result;
        // 生成路由
        await generate(this.menus);
      }
      this.loading = false;
      return response;
    },
  },
});
